import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';

@Component({
  selector: 'app-signin',
  templateUrl: './signin.component.html',
  styleUrls: ['./signin.component.css']
})
export class SigninComponent implements OnInit {

  signinForm = {
    email: '',
    password: '',
  }
  constructor(
    private http:HttpClient,
    private router:Router
    ) { }
  

  ngOnInit(): void {
  }

  signup(){
    this.http.post('http://localhost:3000/session',this.signinForm)
    .toPromise()
    .then((data:any)=>{
      window.localStorage.setItem('auth_token', data.token)
      // 在本地保存用户信息
      window.localStorage.setItem('userinfo', JSON.stringify(data.user));
      // 将其转换为JSON字符串
      this.router.navigate(['/'])
    }).catch(err=>{
      if(err.status === 401){
        window.alert("你的密码或者用户名有错误。请重新输入")
      }else{
        window.alert("不可知的错误出现，请重新输入你的账户密码")
      }
    })
    
  }



}
